<?php
//测试控制器类
class GoodsController extends CommonController{
	private $model;
	public function __init(){
		parent::__init();
		$this->model=K("Goods");
	}
    //动作方法
    public function index(){
    	$data=$this->model->all();
		$this->assign("data",$data);
//		p($data);die;
        //显示视图
        $this->display();
    }
	/**
	 * 删除
	 */
	public function del(){
		$gid=Q("get.gid","","intval");
		
		$goodslist=M("goodslist")->where("goods_gid={$gid}")->find();
		if($goodslist){
			$this->error("请先删除货品");
		}
//		p($gid);die;
		K('Goodsdetail')->where("goods_gid={$gid}")->delete();
		K('Goodsattr')->where("goods_gid={$gid}")->delete();
		$this->model->where("gid={$gid}")->delete();
		$this->success("删除成功");
	}
	/**
	 * 编辑
	 */
	 public function edit(){
	 	$gid=Q("get.gid","","intval");
         //		p($tid);die;
		if(IS_POST){
			if(!$this->model->editdata()) $this->error($this->model->error);
			$this->success("修改成功");
		}
		//处理所属分类
		$c=Data::tree(K('Category')->all(), 'cname');
//         		p($c);die;
		$this->assign('cateData',$c);
		//处理属性规格总数据
//     $cid=K("Goods")->where("gid={$gid}")->getField("category_cid");
//		$tid=K("Category")->where("cid={$cid}")->getField("shop_type_tid");
//		$this->assign('tid',$tid);
//		
//		
//		$attrAll=K('TypeAttr')->where("shop_type_tid={$tid} and class=0")->all();
//		
//		
//		//处理属性value
//			$tmp=array();
//			foreach ($attrAll as $k => $v) {
//				$v['tavalue']=explode(",", $v['tavalue']);
//				$tmp[]=$v;
//			}
//		p($tmp);
//		$this->assign('tmp',$tmp);
//		//规格
//		$specAll=K('TypeAttr')->where("shop_type_tid={$tid} and class=1")->all();
//			//处理属性value
//				$specc=array();
//				foreach ($specAll as $k => $v) {
//					$v['tavalue']=explode(",", $v['tavalue']);
//					$spec[]=$v;
//				}
////			p($specc);
//			$this->assign('spec',$specc);
//		
		//处理所属品牌
		$Brand=K("Brand")->all();
		$this->assign('brand',$Brand);
		//获得旧数据
		$data=M()->join('__goods__ g JOIN __detail__ d ON g.gid=d.goods_gid ')->where("gid={$gid}")->find();
		   
		   $this->assign('data',$data);
//		   p($data);
		//获得旧属性
		   $allattr=M()->join("__goods_attr__ gt JOIN __shop_typeattr__ ta ON gt.shop_typeattr_taid=ta.taid")->where("goods_gid={$gid} and class=0")->all();
		   $attr=array();
		foreach ($allattr as $k => $v) {
			$v['tavalue']=explode(",", $v['tavalue']);
			$attr[]=$v;
		}
		   $this->assign('allattr',$attr);
//		   p($attr);
		   //获得旧规格
		  $allspec=M()->join("__goods_attr__ gt JOIN __shop_typeattr__ ta ON gt.shop_typeattr_taid=ta.taid")->where("goods_gid={$gid} and class=1")->all();
		     $spec=array();
		foreach ($allspec as $k => $v) {
			$v['tavalue']=explode(",", $v['tavalue']);
			$spec[]=$v;
		}
		   $this->assign('allapec',$spec);
//		   p($spec);

        //  处理详情
           $detail=K("Goodsdetail")->where("goods_gid={$gid}")->find();
		   $photo=isset ($detail['photo']) ? $detail['photo'] : array();
		   $detail['photo']=explode(',', $photo);
//		   p($detail);
           $this->assign('detail',$detail);
		   
		   
	 	$this->display();
	 }
	
	/**
	 * 添加
	 */
	public function add(){
		if(IS_POST){
		 if(!$this->model->addgoods()) $this->error($this->model->error);
			$this->success("添加成功");
		
		}
		//处理所属分类
		$c=Data::tree(K('Category')->all(), 'cname');
//		p($c);die;
		$this->assign('cateData',$c);
		//处理所属品牌
		$Brand=K("Brand")->all();
		$this->assign('brand',$Brand);
		
        //显示视图
        $this->display();
    }
	/**
	 * 上传列表图
	 */
	 public function upload(){
	 	$upload=new Upload('Upload/goods'.date('y/m'));
		$file=$upload->upload();
		if(empty($file)){
			$this->ajax('上传失败');
		}else{
			$data=$file[0];
			$this->ajax($data);
		}
	 }
	 /**
	 * 上传图册
	 */
	 public function uploadphoto(){
	 	$upload=new Upload('Upload/photo'.date('y/m'));
		$file=$upload->upload();
		if(empty($file)){
			$this->ajax('上传失败');
		}else{
			$data=$file[0];
			$image=new Image;
			$smallPath = dirname($data['path']) . '/small_' . basename($data['path']);
			$image->thumb($data['path'],$smallPath,72,72);
			$midPath = dirname($data['path']) . '/mid_' . basename($data['path']);
			$image->thumb($data['path'],$midPath,428,428);					
			$this->ajax($data);
		}
	 }
	 
	 	
	 /**
	  * 属性规格异步
	  */
	  public function attrajax(){
	  	$cid=Q("post.cid");
		$cate=K("Category")->where("cid={$cid}")->find();
		$tid=$cate['shop_type_tid'];
		$data=K("typeAttr")->where("shop_type_tid={$tid}")->all();
		
		$tmp=array();
		foreach ($data as $k => $v) {
			$v['tavalue']=explode(",", $v['tavalue']);
			$tmp[]=$v;
		}
	  	echo json_encode($tmp);die();
	  }
	
}